home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / lib / flyEngine / flyLight.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-09  |  3.6 KB  |  168 lines

  1. #include "..\Fly3D.h"
  2.  
  3. void light_vertex::add_light(vector& p,vector& c,float r)
  4. {
  5.     if (nlights<MAX_HWLIGHTS)
  6.     {
  7.         pos[nlights]=p;
  8.         pos[nlights].w=1.0f;
  9.         color[nlights]=c;
  10.         color[nlights].w=1.0f;
  11.         radius[nlights]=r;
  12.         nlights++;
  13.     }
  14. }
  15.  
  16. void light_vertex::init_draw(bsp_object *obj)
  17. {
  18.     glEnable(GL_LIGHTING);
  19.     for( int i=0;i<nhwlights;i++ )
  20.     {
  21.         if (i>=nlights)
  22.             glDisable(GL_LIGHT0+i);
  23.         else 
  24.         {
  25.             flyengine->excludecollision=obj;
  26.             if (flyengine->collision_test(flyengine->bsp,pos[i],obj->pos))
  27.                 {
  28.                 flyengine->excludecollision=0;
  29.                 glDisable(GL_LIGHT0+i);
  30.                 }
  31.             else
  32.                 {
  33.                 flyengine->excludecollision=0;
  34.                 glEnable(GL_LIGHT0+i);
  35.                 glLightfv(GL_LIGHT0+i,GL_POSITION,&pos[i].x);
  36.                 glLightf(GL_LIGHT0+i,GL_LINEAR_ATTENUATION,1.0f/radius[i]);
  37.                 glLightfv(GL_LIGHT0+i,GL_DIFFUSE,&color[i].x);
  38.                 glLightfv(GL_LIGHT0+i,GL_SPECULAR,&color[i].x);
  39.                 }
  40.         }
  41.     }    
  42.     if (obj && obj->node)
  43.     {
  44.         vector ambient=obj->node->color*0.75f;
  45.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT,&ambient.x);
  46.     }
  47. }
  48.  
  49. void light_vertex::end_draw()
  50. {
  51.     glDisable(GL_LIGHTING);
  52.     nlights=0;
  53. }
  54.  
  55. void light_map::set_base(face *f,light_map_pic *lmp,vector& pos)
  56. {
  57.     v0 = *f->vert[0] + pos;
  58.     v1 = *f->vert[1] - *f->vert[0];
  59.     v2 = *f->vert[2] - *f->vert[0];
  60.     uv[0][0]=f->lmuv[0][0];
  61.     uv[0][1]=f->lmuv[0][1];
  62.     uv[1][0]=f->lmuv[1][0]-f->lmuv[0][0];
  63.     uv[1][1]=f->lmuv[1][1]-f->lmuv[0][1];
  64.     uv[2][0]=f->lmuv[2][0]-f->lmuv[0][0];
  65.     uv[2][1]=f->lmuv[2][1]-f->lmuv[0][1];
  66.     det=uv[1][0]*uv[2][1]-uv[2][0]*uv[1][1];
  67.     normal=f->normal;
  68.     vector p1,p2;
  69.     map_point((float)offsetx/lmp->sizex,(float)offsety/lmp->sizey,d0);
  70.     map_point((float)(offsetx+sizex)/lmp->sizex,(float)offsety/lmp->sizey,p1);
  71.     map_point((float)offsetx/lmp->sizex,(float)(offsety+sizey)/lmp->sizey,p2);
  72.     d1=p1-d0;
  73.     d2=p2-d0;
  74. }
  75.  
  76. void light_map::illum(vector& pos,vector& color,float rad,int shadows)
  77. {
  78.     static vector point,dir;
  79.     int i,j,k;
  80.     float dist,fi=1.0f/sizex,fj=1.0f/sizey,fy=fj*0.5f;
  81.     vector v1=d0+d1*(fi*0.5f),v2=d1*fi;
  82.     unsigned char *uc;
  83.     
  84.     rad *= rad;
  85.  
  86.     for( j=0;j<sizey;j++,fy+=fj )
  87.     {
  88.     uc=&bmp[j*bytesx];
  89.     point.x = v1.x + d2.x*fy;
  90.     point.y = v1.y + d2.y*fy;
  91.     point.z = v1.z + d2.z*fy;
  92.     for( i=0;i<sizex;i++,point+=v2 )
  93.         {
  94.         dist=
  95.             (pos.x-point.x)*(pos.x-point.x)+
  96.             (pos.y-point.y)*(pos.y-point.y)+
  97.             (pos.z-point.z)*(pos.z-point.z);
  98.         if (dist>rad)
  99.             uc+=3;
  100.         else 
  101.             {
  102.             dist=(1.0f-dist/rad)*255.0f;
  103.             dir=point-pos;
  104.             switch(shadows)
  105.                 {
  106.                 case 1:
  107.                     if (flyengine->shadow_obj)
  108.                         if (flyengine->shadow_obj->ray_intersect_test(pos,dir))
  109.                         {
  110.                         uc+=3;
  111.                         continue;
  112.                         }
  113.                     break;
  114.                 case 2:
  115.                     dir.normalize();
  116.                     float dot=-vec_dot(dir,normal);
  117.                     if (dot<0.0f ||
  118.                         flyengine->collision_test(flyengine->bsp,pos,point))
  119.                         {
  120.                         uc+=3;
  121.                         continue;
  122.                         }
  123. //                    dist*=dot;
  124.                     break;
  125.                 }
  126.             k=(int)(color.x*dist)+(int)(*uc);
  127.             *(uc++)=k>255?255:k;
  128.  
  129.             k=(int)(color.y*dist)+(int)(*uc);
  130.             *(uc++)=k>255?255:k;
  131.  
  132.             k=(int)(color.z*dist)+(int)(*uc);
  133.             *(uc++)=k>255?255:k;
  134.             }
  135.         }
  136.     }
  137. }
  138.  
  139. void light_map::map_point(float u, float v, vector &point)
  140. {
  141.     u-=uv[0][0];
  142.     v-=uv[0][1];
  143.     point=
  144.         v0+
  145.         v1*((u*uv[2][1]-uv[2][0]*v)/det)+
  146.         v2*((uv[1][0]*v-u*uv[1][1])/det);
  147. }
  148.  
  149. void light_map::load(light_map_pic *lmp)
  150. {
  151.     int i;
  152.     for( i=0;i<sizey;i++ )
  153.         memcpy(
  154.             &bmp[i*bytesx],
  155.             &lmp->bmp[(i+offsety)*lmp->bytesx+offsetx*lmp->bytespixel],
  156.             sizex*bytespixel);
  157. }
  158.  
  159. void light_map::save(light_map_pic *lmp)
  160. {
  161.     int i;
  162.     for( i=0;i<sizey;i++ )
  163.         memcpy(
  164.             &lmp->bmp[(i+offsety)*lmp->bytesx+offsetx*lmp->bytespixel],
  165.             &bmp[i*bytesx],
  166.             sizex*bytespixel);
  167. }
  168.